Two-Dimensional Arrays

Objectives


Discussion

Multi-Dimensional Arrays

0 1
0 "r0c0" "r0c1"
1 "r1c0" "r1c1"
2 "r2c0" "r2c1"

Declaring and Using Arrays 

Back to top


Demonstration

In this demo we will experiment with two-dimensional arrays.

1.  Open your Unit 5 project and add a new form.

2.  Since we are going to experiment with a two-dimensional array it will be helpful to create a sub to display a 2D array just like we did for the 1D array.  Before proceeding we should mention the benefit of Modules.  Remember a module is a file that you can put code in.  Sometimes it is useful to create modules of general subs and functions.  Then you can include the module in your project and not have to rewrite the subs or functions.  At this point it might be useful for you to create a module and put the DisplayArray sub in it.  If you do make sure that it is Public. Then add the next sub that you are about to write, to this module.  Then in the future if you need a sub to display an array, you could add the module to your project and use these subs.   Remember if you do this you will have to precede the call to your sub with the module name. 

3.  Now add the following sub to your module or form. If you add it to your module make sure that you make it Public instead of Private. This sub displays a 2D array in a messagebox.

Private Sub Display2DArray(ByVal ArrayX As Array)
   Dim i, j As Int16
   Dim sList As String
   For i = 0 To ArrayX.GetLength(0) - 1
      For j = 0 To ArrayX.GetUpperBound(1)
         sList = sList & ArrayX(i, j) & " "
      Next
      sList = sList & vbCrLf 
   Next
   MessageBox.Show(sList)
End Sub

4.  Study this sub.  Notice we used GetLength and GetUpperBound.  Either one is okay; you can use whichever you prefer.  Also, we had to pass in the index of the dimension.  Remember that dimension 0 is the first dimension and dimension 1 is the second dimension.

5.  Now add a button and the following code to the click event for the button:

Dim sTable(,) As String = {{"r0c0", "r0c1"}, {"r1c0", "r1c1"}, {"r2c0", "r2c1"}}
Display2DArray(sTable)
sTable(0, 0) = "NEW1"
sTable(2, 1) = "NEW2"
Display2DArray(sTable)

6.  Run the program and check out the messageboxes.

7.  It is unusual to initialize a variable when we declare it.  So let's create an array and initialize it in loops. We will create an array to hold a multiplication table. Add another button to your form and enter the following code in the click event:

Dim i, j As Int16
Dim iMultTable(9, 9) As Int16
For i = 0 To 9 
   For j = 0 To 9
      iMultTable(i, j) = i * j
   Next
Next
Display2DArray(iMultTable)

8. Run the program and click the button.  Study the code. Notice that we used only a few lines of code to assign 100 pieces of data.  Imagine having to assign each value for all 100 elements. 

Back to top
Exercises

1.  Write a statement to declare an array to hold a tic-tac-toe board.  Assume that each element can be an "X", "O", or "-".

2.  Write a statement using loops to initialize each element of your array in #1 to "-".

3.  Write a statement to declare an array to hold the following information.  You do not need to initialize the array; you just need to declare it.  Also assume that the array will hold this information for a class of 20 students.

Name Grade Average
Jones 10 99
Smith 12 88

4.  Write code that initializes a 10 by 10 arrays of characters so that all elements are "X" except those on the diagonal which are set to "O".  If the array is displayed it would look like:

OXXXXXXXXX
XOXXXXXXXX
XXOXXXXXXX
XXXOXXXXXX
XXXXOXXXXX
XXXXXOXXXX
XXXXXXOXXX
XXXXXXXOXX
XXXXXXXXOX
XXXXXXXXXO

5.  Create a simple tic-tac-toe program using the two-dimensional array from #1 to hold the grid.  Also use the sub Display2DArray to display the grid after someone makes a change.  You should enable the user to select the position that they want to change based on the row and column and then select what that position should be changed to  ("X" or "O").  After the person changes a position, the grid should be displayed. 

6.  [Optional] Modify your program in #5 to check for a winner after each turn.

Back to top
Links & Help
Back to top